package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.repository.TravelCommentRepository;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
@Service
public class TravelCommentServiceImpl implements ITravelCommentService {
    @Autowired
    private TravelCommentRepository travelCommentRepository;

    @Override
    public void save(TravelComment travelComment) {
        travelComment.setId(null);
        travelCommentRepository.save(travelComment);
    }

    @Override
    public void delete(String id) {
        travelCommentRepository.deleteById(id);
    }

    @Override
    public void commentAdd(TravelComment comment, UserInfo userInfo) {
        comment.setCreateTime(new Date());
        String refId = comment.getRefComment().getId();
        //维护关联属性
        if (StringUtils.hasLength(refId)) {
            TravelComment refcomment = this.get(refId);
            refcomment.setType(TravelComment.TRAVLE_COMMENT_TYPE);
            comment.setRefComment(refcomment);
        } else {
            comment.setType(TravelComment.TRAVLE_COMMENT_TYPE_COMMENT);
        }
        comment.setUserId(userInfo.getId());
        BeanUtils.copyProperties(userInfo,comment);
        this.save(comment);
    }

    @Override
    public List<TravelComment> queryTravel(String travelId) {
        List<TravelComment> list = travelCommentRepository.findByTravelId(travelId);
        return list;
    }

    @Override
    public void update(TravelComment travelComment) {
        travelCommentRepository.save(travelComment);
    }

    @Override
    public TravelComment get(String id) {
        return travelCommentRepository.findById(id).orElse(null);
    }

    @Override
    public List<TravelComment> list() {
        return travelCommentRepository.findAll();
    }
}
